<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierCode">
              <a-input v-model="model.supplierCode" placeholder="提交后自动生成" disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName">
              <a-input v-model="model.supplierName" placeholder="请输入供应商名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contact">
              <a-input v-model="model.contact" placeholder="请输入联系人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系电话" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-input v-model="model.note" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="法定代表人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="legalRepresentativeName">
              <a-input v-model="model.legalRepresentativeName" placeholder="请输入法定代表人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="委托代理人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appointedAgentName">
              <a-input v-model="model.appointedAgentName" placeholder="请输入委托代理人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="税号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxCode">
              <a-input v-model="model.taxCode" placeholder="请输入税号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bank">
              <a-input v-model="model.bank" placeholder="请输入开户行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="开户行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCode">
              <a-input v-model="model.bankCode" placeholder="请输入开户行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="电子承兑开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCode">
              <a-input v-model="model.eleBank" placeholder="请输入电子承兑开户行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="电子承兑账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCode">
              <a-input v-model="model.eleBankCode" placeholder="请输入电子承兑账号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="邮政编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipCode">
              <a-input v-model="model.zipCode" placeholder="请输入邮政编码" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="供应商信息子表" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="supplierInformationDetailTable.loading"
          :columns="supplierInformationDetailTable.columns"
          :dataSource="supplierInformationDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import { ajaxGetDictItems } from '@api/api'

  export default {
    name: 'SupplierInformationForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
          supplierType: 1, // 设置默认值为1，不在页面显示
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           supplierName: [
              { required: true, message: '请输入供应商名称!'},
           ],
           // contact: [
           //    { required: true, message: '请输入联系人!'},
           // ],
           // phone: [
           //    { required: true, message: '请输入联系电话!'},
           // ],
           // address: [
           //    { required: true, message: '请输入地址!'},
           // ],
        },
        refKeys: ['supplierInformationDetail', ],
        tableKeys:['supplierInformationDetail', ],
        activeKey: 'supplierInformationDetail',
        // 供应商信息子表
        supplierInformationDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料编码',
              key: 'materialCode',
              type: JVXETypes.popup,
              popupCode:"procurement_supplier_erp_detail",
              orgFields:"code,name,none_spec,me_unit,au_unit,co_rate,id",
              destFields:"materialCode,materialName,specification,mainUnit,deputyUnit,conversionRate,materialId",
              field:"code,name,none_spec,me_unit,au_unit,co_rate,",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料名称',
              key: 'materialName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '规格型号',
              key: 'specification',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
            {
              title: '主单位',
              key: 'mainUnit',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              formatter: ({cellValue}) => {
                const item = this.jiUnit.find(item => item.value === cellValue);
                return item ? item.text : cellValue;
              },
              disabled: true,
            },
            {
              title: '辅单位',
              key: 'deputyUnit',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              formatter: ({cellValue}) => {
                const item = this.zlUnit.find(item => item.value === cellValue);
                return item ? item.text : cellValue;
              },
              disabled: true,
            },
            {
              title: '换算率',
              key: 'conversionRate',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled: true,
            },
          ]
        },
        url: {
          add: "/procurement/supplierInformation/add",
          edit: "/procurement/supplierInformation/edit",
          queryById: "/procurement/supplierInformation/queryById",
          supplierInformationDetail: {
            list: '/procurement/supplierInformation/querySupplierInformationDetailByMainId'
          },
        },
        jiUnit: [],
        zlUnit: [],
        isDictLoaded:false
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       this.loadDictData();
    },
    methods: {
      loadDictData() {
        console.log("字典准备加载")
        ajaxGetDictItems('ji_unit', null).then((res) => {
          if (res.success) {
            this.jiUnit = res.result
            console.log("字典1加载完成")
            this.isDictLoaded=true
          }
        })
        ajaxGetDictItems('zl_unit',null).then((res)=>{
          if(res.success){
            this.zlUnit = res.result
            console.log("字典2加载完成")
          }
        })
      },
      addBefore(){
        this.supplierInformationDetailTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      async editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          while (!this.isDictLoaded) {
            await new Promise(resolve => setTimeout(resolve, 100));
            console.log("子表数据加载中...")
          }
          this.requestSubTableData(this.url.supplierInformationDetail.list, params, this.supplierInformationDetailTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          supplierInformationDetailList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>